﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_Hotel_Management_System.Class;
using System.Xml;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class PaymentCheckOut : Form
    {
        public PaymentCheckOut()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (LogInClass.position == "Administrator")
            {
                Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                this.Hide();
                mainA.Show();
            }
            else
            {
                Main__Receptionist_ mainR = new Main__Receptionist_();
                this.Hide();
                mainR.Show();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
        }

        private void PaymentCheckOut_Load(object sender, EventArgs e)
        {
            dataGridView3.Rows.Add("Mineral Water", "1.50", "0");
            dataGridView3.Rows.Add("Coca Cola", "2.50", "0");
            dataGridView3.Rows.Add("Sprite", "2.50", "0");
            dataGridView3.Rows.Add("Tiger Beer", "7.00", "0");
            dataGridView3.Rows.Add("Carlsberg Beer", "7.00", "0");
            dataGridView3.Rows.Add("M&M Chocolate", "3.00", "0");
            dataGridView3.Rows.Add("Dairy Milk Chocolate", "3.50", "0");
            dataGridView3.Rows.Add("Tom Yam Cup Noodles", "4.00", "0");
            dataGridView3.Rows.Add("Chicken Cup Noodes", "4.00", "0");
            dataGridView3.Rows.Add("Lays Potato Chips", "3.00", "0");
            dataGridView3.Rows.Add("Ruffles Potato Chips", "3.00", "0");

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<RoomBooking> roomBookingList = DBManager.getAllRoomBooking();
            listBox1.Items.Clear();
            for (int i = 0; i < roomBookingList.Count; i++)
            {
                listBox1.Items.Add(roomBookingList[i].bookingId);
            }
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                string bookingId = Convert.ToString(listBox1.SelectedItem);
                List<RoomBooking> RoomBookingList = DBManager.getAllRoomBookingByBookingId(bookingId);
                for (int i = 0; i < RoomBookingList.Count; i++)
                {
                    if (listBox1.SelectedItem.Equals(RoomBookingList[i].bookingId))
                    {
                        dataGridView1.RowHeadersVisible = false;
                        dataGridView1.DataSource = RoomBookingList;
                        dataGridView1.Columns["nric"].Visible = false;

                        string nric = dataGridView1.Rows[0].Cells[1].Value.ToString();
                        List<Guest> GuestList = DBManager.getAllGuestbyNric(nric);
                        for (int j = 0; j < GuestList.Count; j++)
                        {
                            dataGridView2.RowHeadersVisible = false;
                            dataGridView2.DataSource = GuestList;
                        }
                    }
                }
            }
        }

        private void btnCheckout_Click(object sender, EventArgs e)
        {
            DialogResult result1 = MessageBox.Show("Create Invoice and Check Out for" + listBox1.SelectedItem + "?","Check Out Confirmation",MessageBoxButtons.YesNo);
            if (result1 == DialogResult.Yes)
            {
                double totalRoomPrice = 0.0;
                double totalAdditionalCharges = 0.0;
                lblInvoiceID.Text = "I" + DateTime.Now.GetHashCode().ToString();
                RoomBooking rb = DBManager.getSingleRoomByBookingId(Convert.ToString(listBox1.SelectedItem));
                Invoice i = new Invoice();
                i.invoiceID = lblInvoiceID.Text;

                foreach (DataGridViewRow r in dataGridView1.Rows)
                {
                    DateTime checkIn = Convert.ToDateTime(r.Cells["checkInDate"].Value);
                    DateTime checkOut = Convert.ToDateTime(r.Cells["checkOutDate"].Value);
                    TimeSpan nights = checkOut - checkIn;
                    i.bookingID = Convert.ToString(listBox1.SelectedItem);
                    i.checkInDate = Convert.ToString(r.Cells["checkInDate"].Value);
                    i.checkOutDate = Convert.ToString(r.Cells["checkOutDate"].Value);
                    i.noOfNights = nights.Days;
                    i.roomType = Convert.ToString(r.Cells["roomType"].Value);
                    i.roomRate = DBManager.getRoomRate(i.roomType);
                    i.paymentMethod = Convert.ToString(r.Cells["paymentMethod"].Value);
                    totalRoomPrice = i.noOfNights * i.roomRate;
                }

                foreach (DataGridViewRow g in dataGridView2.Rows)
                {
                    i.lastname = Convert.ToString(g.Cells["lastName"].Value);
                    i.firstName = Convert.ToString(g.Cells["firstName"].Value);
                    i.email = Convert.ToString(g.Cells["email"].Value);
                    i.phoneNo = Convert.ToInt32(g.Cells["phoneNum"].Value);
                    i.address = Convert.ToString(g.Cells["address"].Value);
                }

                foreach (DataGridViewRow r in dataGridView3.Rows)
                {
                    double price = Convert.ToDouble(r.Cells["Price"].Value);
                    int quantity = Convert.ToInt32(r.Cells["Quantity"].Value);
                    totalAdditionalCharges += price * quantity;
                }


                i.additionalCharges = totalAdditionalCharges;
                i.totalPrice = totalAdditionalCharges + totalRoomPrice;

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter wrt = XmlWriter.Create("F:\\" + lblInvoiceID.Text + ".xml", settings);
                wrt.WriteStartDocument();
                wrt.WriteStartElement("invoice");
                wrt.WriteElementString("invoice_id", lblInvoiceID.Text);
                wrt.WriteElementString("booking_id", i.bookingID);
                wrt.WriteElementString("first_name", i.firstName);
                wrt.WriteElementString("last_name", i.lastname);
                wrt.WriteElementString("email", i.email);
                wrt.WriteElementString("phone_no", Convert.ToString(i.phoneNo));
                wrt.WriteElementString("address", i.address);
                wrt.WriteElementString("check_in_date", i.checkInDate);
                wrt.WriteElementString("check_out_date", i.checkOutDate);
                wrt.WriteElementString("no_of_nights", Convert.ToString(i.noOfNights));
                wrt.WriteElementString("room_type", i.roomType);
                wrt.WriteElementString("room_rate", Convert.ToString(i.roomRate));
                wrt.WriteElementString("additional_charges", Convert.ToString(i.additionalCharges));
                wrt.WriteElementString("total_Price", Convert.ToString(i.totalPrice));
                wrt.WriteElementString("payment_method", i.paymentMethod);
                // close the file
                wrt.WriteEndElement();
                wrt.Flush();
                wrt.Close();

                if (DBManager.CreateInvoice(i) > 0)
                {
                    Room update = new Room();
                    update.roomNum = rb.roomAllocated;
                    update.roomStatus = "Vacant but cleaning";
                    update.roomStartingDate = "";
                    update.roomEndingDate = "";
                    update.noOfAdult = 0;
                    update.noofChild = 0;
                    if (DBManager.UpdateRoomUponCheckOut(rb.roomAllocated, update) == true)
                    {
                        MessageBox.Show("Invoice Created and Room Status Updated!");
                        if (LogInClass.position == "Administrator")
                        {
                            Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                            this.Hide();
                            mainA.Show();
                        }
                        else
                        {
                            Main__Receptionist_ mainR = new Main__Receptionist_();
                            this.Hide();
                            mainR.Show();
                        }
                    }
                }

                else
                {
                    MessageBox.Show("Failed to create invoice");
                }
            }

        }
    }
}
